<template>
<!-- 编辑个人资料 -->
	<view class="content">
		<form @submit="formSubmit" report-submit='true'>
			<view class='personal-data'>
				<view class='list'>
					<view class="item acea-row row-between-wrapper">
						<!-- 头像 -->
						<view class="f13">{{i18n.user_page.edit_user_info.t1}}</view>
						<view class="pictrue" @click.stop='uploadpic'>
							<image :src='userInfo.localPath'></image>
							<image src='../../../static/images/alter.png' class="alter"></image>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view class="f13">
							<!-- 昵称 -->
							{{i18n.user_page.edit_user_info.t2}}
						</view>
						<view class='input'><input type='text' class="f13" name='nickname' :value='userInfo.nick_name'></input></view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view class="f13">
							<!-- 手机号码 -->
							{{i18n.user_page.edit_user_info.t3}}
						</view>
						<navigator url="/pages/users/user_phone/index" hover-class="none" class="input f13" v-if="!userInfo.user_tel">
							<!-- 点击绑定手机号 -->
							<text class="f13">{{i18n.user_page.edit_user_info.t4}}</text>
							<text class="iconfont icon-xiangyou"></text>
						</navigator>
						<view class='input acea-row row-between-wrapper' v-else>
							<input type='text' disabled='true' name='phone' :value='userInfo.user_tel' class='id f13'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view class="f13">
							<!-- email -->
							{{i18n.user_page.edit_user_info.t17}}
						</view>
						<view class='input'>
							<input type='text' class="f13" name='email' :value='userInfo.email'></input>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view class="f13">
							<!-- ID号 -->
							{{i18n.user_page.edit_user_info.t5}}
						</view>
						<view class='input acea-row row-between-wrapper'>
							<input type='text' :value='userInfo.id' disabled='true' class='id f13'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					<!-- #ifdef MP -->
					<view class='item acea-row row-between-wrapper'>
						<view class="f13">
							<!-- 权限设置 -->
							{{i18n.user_page.edit_user_info.t6}}
						</view>
						<view class="input f13" @click="Setting">
							<!-- 点击管理 -->
							{{i18n.user_page.edit_user_info.t7}}
							<text class="iconfont icon-xiangyou"></text>
						</view>
					</view>
					<!-- #endif -->
					<!-- <view class="item acea-row row-between-wrapper" v-if="userInfo.user_tel">
						<view class="f13"> -->
							<!-- 密码 -->
							<!-- {{i18n.user_page.edit_user_info.t8}}
						</view> -->
						<!-- <navigator url="/pages/users/user_pwd_edit/index" hover-class="none" class="input f13"> -->
							<!-- 点击修改密码 -->
							<!-- <text class="f13">
								{{i18n.user_page.edit_user_info.t9}}
							</text>
							<text class="iconfont icon-xiangyou f13"></text>
						</navigator> -->
					<!-- </view> -->
					<view  class="item acea-row row-between-wrapper" v-if="userInfo.user_tel">
						<view class="f12">
							<!-- 允许查看我的抽奖信息 -->
							{{i18n.user_page.edit_user_info.t10}}
						</view>
						<van-switch v-model="checked" size="18px" active-color="#e93323" />
					</view>
				</view>
				<button class='modifyBnt bg-color' formType="submit">
					<!-- 保存修改 -->
					{{i18n.user_page.edit_user_info.t11}}
				</button>
				<!-- #ifdef H5 -->
				<view class="logOut cart-color acea-row row-center-wrapper" @click="outLogin" v-if="!this.$wechat.isWeixin()">
					<!-- 退出登录 -->
					{{i18n.user_page.edit_user_info.t12}}
				</view>
				<!-- #endif -->
			</view>
		</form>
		<!-- #ifdef MP -->
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		userEdit,
		getLogout
	} from '@/api/user.js';
	
	import {
		getUserInfo,
		updateUserHeader,
		updateNickName,
		logout
	} from '@/api/lottery.js';
	import {
		switchH5Login
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import dayjs from "@/plugin/dayjs/dayjs.min.js";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	export default {
		components: {
			// #ifdef MP
			authorize
			// #endif
		},
		data() {
			return {
				userInfo: {},
				loginType: 'h5', //app.globalData.loginType
				userIndex: 0,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false ,//是否隐藏授权
				checked:true //默认是否查看抽奖信息开关
			};
		},
		computed: {
			i18n() {
				return this.$t('index')
			},
			...mapGetters(['isLogin'])
		},
		onReady: function() {
			uni.setNavigationBarTitle({
				title: this.i18n.user_page.edit_user_info.t18
			});
		},
		onLoad() {
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				// #ifdef H5 || APP-PLUS
				toLogin();
				// #endif 
				// #ifdef MP
				this.isAuto = true;
				this.$set(this, 'isShowAuth', true)
				// #endif
			}
		},
		methods: {
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUserInfo();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 小程序设置
			 */
			Setting: function() {
				uni.openSetting({
					success: function(res) {
						console.log(res.authSetting)
					}
				});
			},
			/**
			 * 退出登录
			 * 
			 */
			outLogin: function() {
				let that = this;
				if (that.loginType == 'h5') {
					uni.showModal({
					    title:  this.i18n.user_page.user_payment.t5, // '提示',
					    content: this.i18n.user_page.edit_user_info.t13, // '确认退出登录?',
						cancelText: "Cancel", // 取消按钮的文字  
						confirmText: "Sure",
					    success: function (res) {
					        if (res.confirm) {
								logout().then(res=>{
									that.$store.commit("LOGOUT");
									uni.switchTab({
									    url: '/pages/lottery/index/index'
									});
								}).catch(err => {
					                console.log(err);
									that.$store.commit("LOGOUT");
									uni.switchTab({
									    url: '/pages/lottery/index/index'
									});
					              });
					        } else if (res.cancel) {
					            // console.log('用户点击取消');
					        }
					    }
					});	
				}
			},
			/**
			 * 获取用户详情
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					res.localPath = res.user_header;
					that.$set(that, 'userInfo', res);
				});
			},
			/**
			 * 上传文件
			 * 
			 */
			uploadpic: function() {
				let that = this;
				that.$util.uploadImageOne({url:'personal/updateUserHeader',name:'img'}, function(res){
					that.userInfo.user_header = res.imgPath;
					that.$set(that.userInfo,'localPath',res.imgPath);
					console.log(that.userInfo.localPath)
				});
			},

			/**
			 * 提交修改
			 */
			formSubmit: function(e) {
				let that = this,
					value = e.detail.value
				if (!value.nickname) return that.$util.Tips({
					title: this.i18n.user_page.edit_user_info.t14 // '用户姓名不能为空'
				});
				
				var data={
					nick_name:value.nickname
				};
				updateNickName(data).then(res=>{
					if(res.status=="0"){
						return that.$util.Tips({
							title: this.i18n.user_page.edit_user_info.t15, /// '保存信息成功',
							icon: 'success'
						}, {
							tab: 3,
							url: 1
						});
					}
				}).catch(msg => {
					return that.$util.Tips({	
						title: msg || this.i18n.user_page.edit_user_info.t16 // '保存失败，您并没有修改'
					}, {
						tab: 3,
						url: 1
					});
				});
				// userEdit(value).then(res => {
				// 	return that.$util.Tips({
				// 		title: '更换头像已成功',
				// 		icon: 'success'
				// 	}, {
				// 		tab: 3,
				// 		url: 1
				// 	});
				// }).catch(msg => {
				// 	return that.$util.Tips({	
				// 		title: msg || '保存失败，您并没有修改'
				// 	}, {
				// 		tab: 3,
				// 		url: 1
				// 	});
				// });
			}
		}
	}
</script>

<style scoped lang="scss">
	@import "@/common/font.css";
	.personal-data .wrapper {
		margin: 10rpx 0;
		background-color: #fff;
		padding: 36rpx 30rpx 13rpx 30rpx;
	}

	.personal-data .wrapper .title {
		margin-bottom: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item {
		width: 690rpx;
		height: 160rpx;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		margin-bottom: 22rpx;
		padding: 0 30rpx;
		position: relative;
		border: 2rpx solid #f8f8f8;
		box-sizing:border-box;
	}

	.personal-data .wrapper .wrapList .item.on {
		border-color: #e93323;
		border-radius: 20rpx;
		background-image: url("");
		background-size: 100% 100%;
		background-color: #fff9f9;
		background-repeat: no-repeat;
	}

	.personal-data .wrapper .wrapList .item .picTxt {
		width: 445rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue {
		width: 96rpx;
		height: 96rpx;
		position: relative;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue .alter {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text {
		width: 325rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .name {
		width: 100%;
		font-size: 30rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .phone {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.personal-data .wrapper .wrapList .item .bnt {
		font-size: 24rpx;
		background-color: #fff;
		border-radius: 27rpx;
		width: 140rpx;
		height: 54rpx;
		border: 2rpx solid #e93323;
	}

	.personal-data .wrapper .wrapList .item .currentBnt {
		position: absolute;
		right: 0;
		top: 0;
		font-size: 26rpx;
		background-color: rgba(233, 51, 35, 0.1);
		width: 140rpx;
		height: 48rpx;
		border-radius: 0 20rpx 0 20rpx;
	}

	.personal-data .list {
		// margin-top: 15rpx;
		background-color: #fff;
		width: 96%;
		margin: 20upx auto 0;
		border-radius: 6px;
	}

	.personal-data .list .item {
		padding: 20rpx 18rpx;
		border-bottom: 1rpx solid #f2f2f2;
		// margin-left: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .list .item .phone {
		width: 160rpx;
		height: 56rpx;
		font-size: 24rpx;
		color: #fff;
		line-height: 56rpx;
		border-radius: 32rpx
	}

	.personal-data .list .item .pictrue {
		width: 88rpx;
		height: 88rpx;
		position: relative;
	}

	.personal-data .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}
	
	.personal-data .list .item .pictrue .alter{
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.personal-data .list .item .input {
		width: 415rpx;
		text-align: right;
		color: #868686;
	}

	.personal-data .list .item .input .id {
		width: 365rpx;
	}

	.personal-data .list .item .input .iconfont {
		font-size: 35rpx;
	}

	.personal-data .modifyBnt {
		font-size: 28rpx;
		color: #fff;
		// width: 690rpx;
		width: 50%;
		height: 80rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 80rpx;
		margin: 50rpx auto 0 auto;
	}

	.personal-data .logOut {
		font-size: 28rpx;
		text-align: center;
		// width: 690rpx;
		width: 50%;
		height: 80rpx;
		border-radius: 45rpx;
		margin: 30rpx auto 0 auto;
	}

	.content{
		// padding-top: 5upx;
	}
</style>
